const fs = require('fs')
const path = require('path')

const readFile = (filename, encoding) => new Promise((resolve, reject) => {
  fs.readFile(filename, encoding, (err, data) => {
    if (err) reject(err)
    resolve(data)
  })
})

const writeFile = (filename, data, encoding) => new Promise((resolve, reject) => {
  fs.writeFile(filename, data, encoding, err => {
    if (err) reject(err)
    resolve()
  })
})

// 任务 读取 files/a.txt 写入到  files/b.txt中
async function readAndWrite() {
  try {
    const data = await readFile(path.join(__dirname, './files/a.txt'), 'utf8')
    await writeFile(path.join(__dirname, './files/b.txt'), data, 'utf8')
  } catch (error) {
    console.log('任务执行失败' + error.message)
  }
}
readAndWrite()
